<template>
	<view class="index">
		<view class="tabbar">
			<view class="item" :class="index == typeIndex ? 'active' : ''" @tap="changeType(index)" v-for="(item, index) in typeList" :key="index">
				<text>{{item.name}}</text>
			</view>
		</view>
		
		
		<view class="orders" v-if="typeList[typeIndex]['orders'].length > 0">
			<view class="item" v-for="(item, index) in typeList[typeIndex]['orders']" :key="index" @tap="detail(item)">
				<view class="header">
					<view class="bus-name">{{item.business_name}}</view>
					<view class="order-status text-danger">{{item.order_status_text}}</view>
				</view>
				<view class="goods">
					
					<order-goods :goods="goods" v-for="(goods, goodsIndex) in item.products" :key="goodsIndex"></order-goods>
				</view>
				<view class="count">
					<view class="number">共{{item.goods_number}}件商品</view>
					<view class="price">合计：￥{{item.order_amount}}</view>
				</view>
				
				<view class="footer" v-if="item.order_status >= 0 && item.order_status <= 4">
					<!-- 关闭或完成可删除订单 -->
					<view @tap.stop="del(item, index)" class="btn" v-if="item.order_status == 0 || item.order_status == 4">删除订单</view>
					
					<!-- 未付款可取消订单 -->
					<view @tap.stop="cancel(item, index)" class="btn" v-if="item.order_status == 1">取消订单</view>
					
					<view @tap.stop="pay(item)" class="btn" v-if="item.order_status == 1">付款</view>
					
					<!-- 等待卖家发货 -->
					<view @tap.stop="remind(item)" class="btn" v-if="item.order_status == 2">提醒发货</view>
					
					<!-- 卖家已发货 -->
					<view @tap.stop="shipping(item)" class="btn" v-if="item.order_status == 3 || item.order_status == 4">查看物流</view>
					
					<view @tap.stop="signOrder(item, index)" class="btn danger" v-if="item.order_status == 3">确认收货</view>
					
					
					<!-- 订单完成 -->
					<view @tap.stop="comment(item)" class="btn danger" v-if="item.order_status == 4">评价订单</view>
					
				</view>
				
			</view>
		</view>
		
		<view class="no-data" v-else>没有该状态订单</view>
	</view>
</template>

<script>
	import Request from '../../common/request.js';
	import orderGoods from '../../components/order-goods.vue';
	
	export default {
		components: {
			orderGoods
		},
		data() {
			return {
				typeList: [
					{
						status: '',
						name: '全部订单',
						orders: [],
						page: 1,
						nextPage: true
					},
					{
						status: 1,
						name: '待付款',
						orders: [],
						page: 1,
						nextPage: true
					},
					{
						status: 2,
						name: '待发货',
						orders: [],
						page: 1,
						nextPage: true
					},
					{
						status: 3,
						name: '待收货',
						orders: [],
						page: 1,
						nextPage: true
					},
					{
						status: 4,
						name: '待评价',
						orders: [],
						page: 1,
						nextPage: true
					}
				],
				typeIndex: 0
			};
		},
		onLoad() {
			this.getOrder();
		},
		onReachBottom() {
			this.getOrder();
		},
		methods: {
			//获取订单
			getOrder() {
				var index = this.index;
				if(this.typeList[this.typeIndex]['nextPage'] == false) {
					return false;
				}
				
				var that = this;
				Request.send({
					url: 'order/index',
					data: {
						status: that.typeList[that.typeIndex]['status'],
						page: that.typeList[that.typeIndex]['page'],
					},
					sCallback: function(res) {
						that.typeList[that.typeIndex]['orders'] = that.typeList[that.typeIndex]['orders'].concat(res.data);
						that.typeList[that.typeIndex]['page'] ++ ;
						if(res.data.length < 20) {
							that.typeList[that.typeIndex]['nextPage'] = false;
						}
					}
				})
			},
			//切换订单类型
			changeType(index) {
				this.typeIndex = index;
				if(this.typeList[index]['orders'].length == 0) {
					this.getOrder();
				}
			},
			//删除订单
			del: function(item, index){
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确认删除该订单吗？',
					success(e) {
						if(e.confirm == 1) {
							Request.send({
								url: 'order/delOrder',
								data: {id: item.id},
								sCallback: function(res) {
									that.typeList[that.typeIndex]['orders'].splice(index, 1);
								}
							})
						}
					}
				});
			},
			//取消订单
			cancel: function(item, index){
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确认取消该订单吗？',
					success(e) {
						if(e.confirm == 1) {
							Request.send({
								url: 'order/cancelOrder',
								data: {id: item.id},
								sCallback: function(res) {
									that.typeList[that.typeIndex]['orders'][index]['order_status'] = 0;
								}
							})
						}
					}
				});
				
			},
			//提醒发货
			remind: function(item){
				var that = this;
				Request.send({
					url: 'order/orderRemind',
					data: {order_id: item.id},
					sCallback: function(res) {
						uni.showToast({
							title:res.msg
						})
					}
				});
			},
			//查看物流
			shipping: function(item){
				uni.navigateTo({
					url: '/pages/order/shipping?code='+item.shipping_code+'&no='+item.shipping_no+'&name='+item.shipping_name
				})
				
			},
			//确认收货
			signOrder: function(item, index){
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确认签收该订单吗？',
					success(e) {
						if(e.confirm == 1) {
							Request.send({
								url: 'order/signOrder',
								data: {order_id: item.id},
								sCallback: function(res) {
									console.log(res);
									if(res.code == 200) {
										that.typeList[that.typeIndex]['orders'][index]['order_status'] = 4;
									}
									
								}
							})
						}
					}
				});
				
			},
			//评价订单
			comment: function(item) {
				uni.navigateTo({
					url: '/pages/order/comment?order_id='+item.id
				})
			},
			//订单详情
			detail: function(item) {
				uni.navigateTo({
					url: '/pages/order/detail?order_id='+item.id
				})
			},
			//支付订单
			pay: function(item) {
				uni.navigateTo({
					url: '/pages/order/payorder?type=order&id='+item.id
				})
			}
		}
	}
</script>

<style>
	.index{
		font-size: 28upx;
		padding-top: 44px;
	}
	
	.tabbar{
		background: #fff;
		height: 80upx;
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		top: 0;
		width: 100%;
		z-index: 9;
		border-top: 1upx solid #eee;
		border-bottom: 1upx solid #eee;
	}
	
	/* #ifdef H5 */
		.tabbar{
			top: 44px;
		}
	/* #endif */
	
	.tabbar .item {
		flex: 1;
		text-align: center;
		color: #666;
		height: 80upx;
		display: flex;
		align-items: center;
		justify-content: center;
		/* border-bottom: 2upx solid #fff; */
		
		box-sizing: border-box;
	}
	
	.tabbar .item.active{
		color: #E60012;
		/* border-bottom: 2upx solid #E60012; */
	}
	
	.tabbar .item text{
		border-left: 1upx solid #eee;
		width: 100%;
	}
	
	.tabbar .item:nth-child(1) text{
		border:none;
	}
	.orders .item{
		background: #fff;
		margin-top: 10upx;
	}
	
	.orders .item .header{
		height: 80upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1upx solid #eee;
		padding: 0 20upx;
	}
	
	.orders .count{
		display: flex;
		justify-content: flex-end;
		height: 80upx;
		align-items: center;
		padding: 0 20upx;
		color: #666;
		border-bottom: 1upx solid #eee;
	}
	
	.orders .count .price{
		margin-left: 40upx;
	}

	.orders .footer {
		height: 100upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		padding: 0 20upx;
	}
	
	.orders .footer .btn{
		height: 60upx;
		border: 1upx solid #999;
		display: flex;
		align-items: center;
		font-size: 24upx;
		padding: 0 20upx;
		border-radius: 10upx;
		margin-left: 20upx;
	}
	
	.orders .footer .btn.danger{
		color: #E60012;
		border-color: #E60012;
	}
</style>
